<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Node
 *
 * @property int $id
 * @property string $name 节点名称
 * @property string $route_name 路由别名，权限标识
 * @property int $pid 上级id
 * @property string $is_menu 是否是菜单，0不是，1是
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereIsMenu($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Node whereRouteName($value)
 * @mixin \Eloquent
 */
class Node extends Common
{
    //修改器
    public function setRouteNameAttribute($value)
    {
        $this->attributes['route_name']=empty($value)?'':$value;
    }


    // 获取全部的数据
    public function getAllList() {
        $data = self::get()->toArray();
        return $this->treeLevel($data);
    }

    /**
     * 获取层级的数据
     * @return array
     */
    public function treeData(array $allow_node) {
        $query=Node::where('is_menu', '1');
        //判断是否超级管理员
        if (auth()->id() != config('web.Administrators')){
            $query->whereIn('id',$allow_node);
        }
        $menuData=$query->get()->toArray();
        return $this->subTree($menuData);
    }



}
